package com.usian.controller;

import com.usian.pojo.*;
import com.usian.service.ItemCategoryService;
import com.usian.service.ItemParamService;
import com.usian.service.ItemService;
import com.usian.utils.CatResult;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
public class ItemController {

    @Autowired
    private ItemParamService itemParamService;

    @Autowired
    private ItemCategoryService itemCategoryService;

    @Autowired
    private ItemService itemService;

    /**
     * 修改商品
     * @param tbItem
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("updateTbItem")
    public Integer updateTbItem(@RequestBody TbItem tbItem, @RequestParam String desc, @RequestParam String itemParams){
        return itemService.updateTbItem(tbItem,desc,itemParams);
    }

    /**
     * 修改回显
     * @param itemId
     * @return
     */
    @RequestMapping("preUpdateItem")
    public Map<String,Object> preUpdateItem(@RequestParam Long itemId){
        return itemService.preUpdateItem(itemId);
    }

    /**
     * 测试查看所有
     * @return
     */
    @RequestMapping("findAll")
    public List<TbItem> findAll(){
      return itemService.findAll();
    }

    /**
     * 查看商品列表
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("selectTbItemAllByPage")
    public PageResult selectTbItemAllByPage(@RequestParam Integer page, @RequestParam Integer rows){
        return itemService.pageFindAll(page,rows);
    };

    /**
     * 根据id查询
     * @param itemId
     * @return
     */
    @RequestMapping("findById")
    public TbItem findById(@RequestParam Long itemId){
      return itemService.findById(itemId);
    }

    /**
     * 测试删除
     * @param itemId
     */
    @RequestMapping("delete")
    public void delete(@RequestParam Long itemId){
        itemService.delete(itemId);
    }

    /**
     * 根据id查询
     * @param itemId
     */
    @RequestMapping("deleteItemById")
    public Integer deleteItemById(@RequestParam Long itemId){
       return itemService.deleteItemById(itemId);
    }

    /**
     * 查看商品类目
     * @param id
     * @return
     */
    @RequestMapping("selectItemCategoryByParentId")
    List<TbItemCat> selectItemCategoryByParentId(@RequestParam Integer id){
        return itemCategoryService.selectItemCategoryByParentId(id);
    };

    /**
     * 查询商品规格参数模板
     * @param itemCatId
     * @return
     */
    @RequestMapping("/selectItemParamByItemCatId/{itemCatId}")
    public TbItemParam selectItemParamByItemCatId(@PathVariable Long itemCatId) {
        return itemParamService.selectItemParamByItemCatId(itemCatId);
    }

    /**
     * 添加商品
     * @param tbItem
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("/insertTbItem")
    public Integer insertTbItem(@RequestBody TbItem tbItem, @RequestParam String desc, @RequestParam String itemParams){
        return itemService.insertTbItem(tbItem,desc,itemParams);
    }

    /**
     * 规格模板查询
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/selectItemParamAll")
    public PageResult selectItemParamAll(@RequestParam Integer page,@RequestParam Integer rows){
        return itemParamService.selectItemParamAll(page,rows);
    }

    /**
     * 商品规格模板添加
     * @param itemCatId
     * @param paramData
     * @return
     */
    @RequestMapping("/insertItemParam")
    public Integer insertItemParam(@RequestParam Long itemCatId,@RequestParam String paramData){
        return itemParamService.insertItemParam(itemCatId,paramData);
    }

    /**
     * 商品规格模板删除
     * @param id
     * @return
     */
    @RequestMapping("deleteItemParamById")
    public Integer deleteItemParamById(@RequestParam Long id){
        return itemParamService.deleteItemParamById(id);
    }

    /**
     * 首页左侧商品分类
     * @return
     */
    @RequestMapping("selectItemCategoryAll")
    public CatResult selectItemCategoryAll(){
        return itemService.selectItemCategoryAll();
    }

    /**
     * 商品详情
     * @param itemId
     * @return
     */
    @RequestMapping("selectItemInfo")
    public TbItem selectItemInfo(@RequestParam Long itemId){
        return itemService.selectItemInfo(itemId);
    }

    /**
     * 商品详情页 商品介绍
     * @param itemId
     * @return
     */
    @RequestMapping("selectItemDescByItemId")
    public TbItemDesc selectItemDescByItemId(@RequestParam Long itemId){
        return itemService.selectItemDescByItemId(itemId);
    }

    /**
     * 商品详情页 规格参数
     * @param itemId
     * @return
     */
    @RequestMapping("selectTbItemParamItemByItemId")
    public TbItemParamItem selectTbItemParamItemByItemId(@RequestParam Long itemId){
        return itemService.selectTbItemParamItemByItemId(itemId);
    }
}
